package app.v1.schedulers;

import app.common.utils.WdtUtils;
import app.common.utils.YdApi;
import app.common.utils.cjt.ao.WmsStockinOtherQueryWithDetailAo;
import app.common.utils.cjt.vo.WmsStockinOtherQueryWithDetailVo;
import app.common.utils.dingtalk.domain.SearchCondition;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDResponseBody;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 更新码rc调度器
 * 每六分钟从旺店通更新一次库存到宜搭表单（FORM-MFA66S9170Z4L21R8XKYM7CP6TWB2CD4DJN9L6）
 * @author feizns
 * @since 2023/07/18
 */
@Component
public class UpdateToYdRcScheduler {

    /**
     * 更新码rc
     */
    @Scheduled(cron = "0 0/6 * * * ?")
    public void updateToYdRc() {
        LocalDateTime yesterday = LocalDate.now().minusDays(1).atStartOfDay();
        WmsStockinOtherQueryWithDetailAo ao = new WmsStockinOtherQueryWithDetailAo(yesterday);
        try {
            WdtUtils.wmsStockinOtherQueryWithDetail(ao, this::update);
            //FORM-MFA66S9170Z4L21R8XKYM7CP6TWB2CD4DJN9L6
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新
     * @param order 订单
     */
    private void update(WmsStockinOtherQueryWithDetailVo.Order order) {
        List<String> list = YdApi.yiDaUtils.forms.instanceIds("FORM-FV966U917NGCEMCF670LEA1W373039GPAY1KL4", SearchCondition.textEq("textField_lk1yb6ih", order.getOrder_no()));
        for (String id : list) {
            GetFormDataByIDResponseBody data = YdApi.yiDaUtils.forms.getById(id);
            Map<String, ?> formData = data.getFormData();
            if ( Objects.isNull(formData.get("numberField_lk1yb6in")) ) {
                try {
                    YdApi.yiDaUtils.forms.updateById(data.getFormInstId(), Map.of("numberField_lk1yb6in", order.getGoods_count()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //更新领料、料、报工关联表
                List<WmsStockinOtherQueryWithDetailVo.Order.DetailList> detailList = order.getDetail_list();
                for (WmsStockinOtherQueryWithDetailVo.Order.DetailList detail : detailList) {
                    List<String> ids = YdApi.yiDaUtils.forms.instanceIds("FORM-MFA66S9170Z4L21R8XKYM7CP6TWB2CD4DJN9L6",
                            SearchCondition.textEq("textField_l88k8548", (String) data.getFormData().get("textField_lk1yb6ir")),
                            SearchCondition.textEq("textField_l7ipbu3u", detail.getSpec_no())
                    );
                    //添加次数
                    for (String s : ids) {
                        GetFormDataByIDResponseBody a = YdApi.yiDaUtils.forms.getById(s);
                        Long num = (Long) a.getFormData().get("numberField_ldtlupsm");
                        if ( num == null )
                            num = 0L;
                        num += detail.getGoods_count();
                        YdApi.yiDaUtils.forms.updateById(s, Map.of("numberField_ldtlupsm", num));
                    }
                }
                System.out.printf("更新单号：%s\n", order.getOrder_no());
            }
        }
    }

}
